//
//  WBNavigationController.swift
//  迷你微博
//
//  Created by Json on 2020/2/3.
//  Copyright © 2020 Json. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //隐藏默认NavigationBar
        navigationBar.isHidden = true
    }
    
    //重写push方法，所有的push方法都会调用此方法
    //viewController是被push的控制器，设置他为左侧按钮作为返回按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool){
        //如果不是栈底控制器才需要隐藏，根控制器不需要处理
        if children.count > 0{
            //隐藏底部的TabBar
            viewController.hidesBottomBarWhenPushed = true
            
            //判断控制器类型
            if let vc = viewController as? WBBaseViewController{
                
                var title = "返回"
                
                //判断控制器级别，只有一个控制器的时候，显示栈底控制器标题
                if children.count == 1 {
                    //title显示首页的标题
                    title = children.first?.title ?? "返回"
                }
                
                //取出自定义navItem
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent),isBack:true)
            }
        }
        
        super.pushViewController(viewController,animated:true)
    }

    //pop 返回上一层控制器
    @objc private func popToParent(){
        popViewController(animated: true)
    }
}
